<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://www.ufida.com/lfw" prefix="lfw" %>
<%@ taglib uri="http://www.ufida.com/uif" prefix="uif" %> 
<%@ taglib uri="http://www.ufida.com/lfwtool" prefix="lfwtool" %> 
<%@ taglib uri="http://www.ufida.com/multilang" prefix="ml" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@page import="nc.lfw.core.LfwRuntimeEnvironment"%>
<%@page import="nc.lfw.billtemplate.controller.ILfwControllerBase"%>
<%@page import="nc.lfw.billtemplate.controller.IManageControllerBase"%>
<html>
	<head> 
		<lfw:base/>
		<lfw:head/>
		<lfw:import/> 
	</head> 
	<body bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0" scroll=no onload="pageBodyScript()">
		<lfw:pageModel className="${lfwtool:getInfo('modelClazz')}" showLoadingPage="false">
			<lfw:layout type="border">
				<lfw:layoutPanel position="top" height="50">
					<lfw:menubar id="menubar"></lfw:menubar>
				</lfw:layoutPanel>
				<lfw:layoutPanel position="center">
					<lfw:spliter divideSize="60" boundMode="px" id="spliter1" orientation="v">
						<lfw:spliterPanelOne>
							<lfw:form id="headForm"></lfw:form>
						</lfw:spliterPanelOne>
						<lfw:spliterPanelTwo>
							<lfw:grid id="bodyGrid" />
						</lfw:spliterPanelTwo> 
					</lfw:spliter>	
				</lfw:layoutPanel>
			</lfw:layout>
		</lfw:pageModel>
	</body>
</html>
<script Language="JavaScript">
	<%@ include file="reportutils.jsp"%>
	function externalInit()
	{
		var headDs = getDataset('headDataset');
		headDs.setValueAt(0, headDs.nameToIndex("showtype"), "0");
		headDs.setValueAt(0, headDs.nameToIndex("booktype"), "0");

		var grid = getComponent("bodyGrid");
		if(grid != null)
		{
			grid.outerDiv.style.borderRight = "none";
			grid.outerDiv.style.borderLeft = "none";
			grid.outerDiv.style.borderBottom = "none";
			setGridVisibleHeaders(null, 0, null);
		}

		var menubar = getComponent("menubar");
		if(menubar != null)
		{
			menubar.Div_gen.style.borderTop = "none";
			menubar.Div_gen.style.borderLeft = "none";
			menubar.Div_gen.style.borderRight = "none";
		}
		
		adjustSpliter();
		var action = '<%=request.getParameter("triggertype")%>'
		if(action == 'qrydetail'){
			var loader = new DatasetLoader(getDataset('bodyDataset'));
			loader.setKeyValue(Dataset.MASTER_KEY);
			loader.load();
		}
		var qryObj_str = getSessionAttribute('total_query_condition');
  		var v_map = getSessionAttribute('total_v_map');
  		var other_map = getSessionAttribute('total_other_map');
  		saveQryCondition(qryObj_str,v_map,other_map, false);
  		
  		var vMap = getMap(v_map);
  		var otherMap = getMap(other_map);
	  	if(vMap != null && otherMap != null){
	  		repaintForm(vMap,otherMap);
	  		saveMap(v_map,other_map);
	  	}
	}
	function getMap(mapstr){
		if(mapstr == null || mapstr == '')
			return null
		var tmp1 = mapstr.split('$');
		var arr = eval(tmp1);
		var map = new HashMap();
		for(var i=0;i<arr.length;i++){
			if(arr[i] == null || arr[i] == '')
				continue;
			var tmp2 = arr[i].split(':');
			if(tmp2[1] == null || tmp2[1] == 'null')
				tmp2[1] = '';
			map.put(tmp2[0],tmp2[1]);
		}
		return map;
	}
	function adjustSpliter()
	{
		var form = getComponent("headForm");
		var headHeight = form.getHeight() + 30;
		var headSpliter = getComponent("spliter1");
		headSpliter.fixProp(headHeight);
	}
	
	function afterPageInit() 
	{
  		var headDs = getDataset('headDataset');
  		headDs.setRowSelected(0);

  		headDs.onAfterDataChange = function(event, pageIndex)
  		{	
  			var showTypeIndex = headDs.nameToIndex("showtype");
  			var booktypeIndex = headDs.nameToIndex("booktype");
  			var grid = getComponent("bodyGrid"), row = event.currentRow;
  			// 当前帐页格式值
  			var bookTypeValue = row.getCellValue(booktypeIndex);
  			if(event.cellColIndex == showTypeIndex)
  			{	
  				var showTypeValue = row.getCellValue(showTypeIndex);
  				setGridVisibleHeaders(window.qryObjs, bookTypeValue, showTypeValue);
  			}
  			else if(event.cellColIndex == booktypeIndex){
  				if(window.isFromQuery){
  				    window.isFromQuery = false;
  	  	  			return;
  	  	  		}
  				if(event.oldValue != null && window.qryConditionObj != null){
  					if(event.oldValue != event.currentValue){
		  				var param = "report_query_param=" + window.qryConditionObj;
		  				if(event.currentValue == '1')
		  	  				param += "&fromWhere=jes";
						var pDs = getDataset('bodyDataset');
						var loader = new DatasetLoader(pDs);
						loader.setKeyValue(Dataset.MASTER_KEY);
						loader.setLoadParam(param);
						loader.load();
  					}
  	  			}
  				var bookTypeValue = row.getCellValue(booktypeIndex);
  				setGridVisibleHeaders(window.qryObjs, bookTypeValue, row.getCellValue(showTypeIndex));
  			}
  		}
 	}
 	
 	function setGridVisibleHeaders(qryObjs, bookType, showType)
 	{	
 	 	var visibles = getGridAllVisibleHeaderIds(qryObjs, bookType, showType);
 	 	
		var showHeaders = [], grid = getComponent("bodyGrid");
	
 	 	for(var i = 0, count = grid.basicHeaders.length; i < count; i++)
		{
 	 		if(visibles.indexOf(grid.basicHeaders[i].keyName) != -1)
				showHeaders.push(grid.basicHeaders[i].keyName);
			}
 	 	if(showHeaders.length > 0)
			grid.setShowColumns(showHeaders);
 	}

	/**
	 * qryObjs:当前的查询对象数组
	 * bookType:帐页格式,"0":"本币";"1":"原币","本币"
	 * showType:0:名称,"1":编码,"2":名称+编码
	 */
 	function getGridAllVisibleHeaderIds(qryObjs, bookType, showType)
 	{
 	 	var headers = [];
 	 	if(bookType == "0")
 	 	{
			headers.push("bodydate");
			headers.push("explanation");
			headers.push("billtype");
			headers.push("billno");
			headers.push("currtypename");
 	 	 	headers.push("debitlocalamount");
 	 	 	var headDs = getDataset('headDataset');
			var corpindex = headDs.nameToIndex("headcorp");
			var corpvalue = headDs.getSelectedRow().getCellValue(corpindex);
			if(corpvalue != null)  {
				var num = corpvalue.split(",");
				if(num.length > 1) {
					headers.push("corpname");
				}
			}
 	 	}	
 	 	else if(bookType == "1")
 	 	{
 	 		headers.push("bodydate");
			headers.push("explanation");
			headers.push("billtype");
			headers.push("billno");
 	 	 	headers.push("currtypename");
 	 	 	headers.push("debitamount");
 	 	 	headers.push("debitlocalamount");

 	 	 	var headDs = getDataset('headDataset');
			var corpindex = headDs.nameToIndex("headcorp");
			var corpvalue = headDs.getSelectedRow().getCellValue(corpindex);
			if(corpvalue != null)  {
				var num = corpvalue.split(",");
				if(num.length > 1) {
					headers.push("corpname");
				}
			}
 	 	}
 	 	
 	 	if(qryObjs != null && qryObjs.length > 0)
 	 	{
 	 	 	for(var i = 0; i < qryObjs.length; i++)
 	 	 	{
 	 	 		if(qryObjs[i] == 'corpcode'){
			 	 	headers.push("corpname");
			 	 	continue;
			 	 }
				var index = qryObjs[i].substring(qryObjs[0].length - 1);
				if(showType == "1")
					headers.push("qryobjcode" + index);
				else if(showType == "2")
				{
					headers.push("qryobjcode" + index);
					headers.push("qryobjname" + index);
				}
				else 
					headers.push("qryobjname" + index);	
 	 	 	}
 	 	}
 	 	return headers;
 	}

	/**
	 * 自定义按钮的执行逻辑
	 */
 	function selfDefBtnFuc(btnNo)
	{
		if(btnNo.name == "201")
			doPrint();
		else if(btnNo.name == "202"){
			var url = getCorePath() + "/reportquery/reportquery.jsp?pageId=reportquery&pagetype=detail";
			showDialog(url,"${ml:trans('BxContrastPageModel-000002')}",'700','500','query_dialog',false);
		}else if(btnNo.name == "203"){
			var bodyDs = getDataset('bodyDataset');
			//将明细的查询条件放入session，作为回退到汇总时的条件
			var queryObj =  window.qryConditionObj;
			if(queryObj == null)
				queryObj = getSessionAttribute('total_query_condition');
			var totalIndex = getSessionAttribute('total_query_condition_rowindex');
			if(queryObj == null){
				showMessageDialog("${ml:trans('yer_balanceui002')}");
				return;
			}
			
			var param ="report_query_param=" + encodeURIComponent(queryObj) + "&rowindex=" + totalIndex + "&v_map=" + encodeURIComponent(window.map) + "&other_map=" + encodeURIComponent(window.otherMap);
			var cmd = new CommonCommand('setQryDeteilConToSession');
			cmd.setAsync(false);
			cmd.setParameter(param);
			cmd.exec();
			//processCommonLogic(, "setQryDeteilConToSession", null, null, false);
			var url = getCorePath() + '/balancereportui.jsp?model=nc.lfw.finance.model.ExpenseBalanceReportPageModel&ctrl=nc.lfw.finance.report.FyTotalController&pageId=E0100401&triggertype=qrybalance';
			window.location.href = url;	
		}	
	}

</script>